﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using IDAL;
using System.Data.Linq;

namespace SQLServerDAL
{
    public class UserBalanceDAL : IUserBalanceDAL
    {
        public Boolean InsertUserBalance(UserBalance userBalance)
        {
            try
            {
                UserBalanceDataClassesDataContext userBalanceContext = new UserBalanceDataClassesDataContext();
                userBalanceContext.UserBalances.InsertOnSubmit(userBalance);
                userBalanceContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteUserBalance(int userId)
        {
            try
            {
                UserBalanceDataClassesDataContext userBalanceContext = new UserBalanceDataClassesDataContext();
                UserBalance userBalance = (from u in userBalanceContext.UserBalances
                                     where u.UserId.Equals(userId)
                                     select u).First();
                userBalanceContext.UserBalances.DeleteOnSubmit(userBalance);
                userBalanceContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean UpdateUserBalance(int userId, UserBalance userBalance)
        {

            if (userId != userBalance.UserId)
            {
                return false;
            }
            UserBalanceDataClassesDataContext userBalanceContext = new UserBalanceDataClassesDataContext();
            UserBalance query = (from u in userBalanceContext.UserBalances
                              where u.UserId.Equals(userId)
                              select u).First();
            //query.Password = user.Password;

            userBalanceContext.SubmitChanges();
            return true;

        }


        public UserBalance SelectUserBalanceById(int userId)
        {
            UserBalanceDataClassesDataContext userBalanceContext = new UserBalanceDataClassesDataContext();
            UserBalance userBalance = (from u in userBalanceContext.UserBalances
                                 where u.UserId.Equals(userId)
                                 select u).First();
            return userBalance;
        }


        public List<UserBalance> SelectAllUserBalance()
        {
            UserBalanceDataClassesDataContext userBalanceContext = new UserBalanceDataClassesDataContext();
            var query = (from u in userBalanceContext.UserBalances
                         select u);
            List<UserBalance> userBalanceList = new List<UserBalance>();

            foreach (var userBalance in query)
            {
                userBalanceList.Add(userBalance);
            }
            return userBalanceList;
        }

    }
}
